<?php
/**
 * Created by PhpStorm.
 * User: PC
 * Date: 2017/5/17
 * Time: 18:28
 */
namespace Admin\Controller;


use Think\Cache\Driver\Redis;
use Think\Controller;

class VoteCheatController extends Controller
{
    protected $vote_id;             //投票活动ID
    protected $option_id;           //选手ID
    protected $ranking;             //想要的名次  第一名 0  第二名 1  第三名 2 ...

    public function __construct()
    {
        parent::__construct();
        $this->vote_id   = I('vote_id', 11, 'intval');
        $this->option_id = I('option_id', 619, 'intval');
        $this->ranking   = I('ranking', 2, 'intval');
    }

    /*
 *  30分钟执行一次
 *  计算30分钟内这个人的票数
 *  随机加 最多不超过四票
 * */
    public function addVote(){
        date_default_timezone_set('Asia/Shanghai');

        $total       = I('total', 732);
        $today_total = I('today_total', 500);

        //时间段，早上8-晚上10点为投票时间
        $hour = date('H');
        if($hour < 8 || $hour > 23) exit(1000);

        //计算30分钟内这个人的票数计算
        $vote_user_option_id = '619';
//        $start_time = time() - 2000;
//        $end_time   = time();
//        //最近三十分钟他的投票总数
//        $period_count = M('vrnews1.vote_ballot')
//            ->where("option_id={$vote_user_option_id} AND create_time>{$start_time} AND create_time<{$end_time}")
//            ->count();

//        if($period_count > 20) exit(1001);

        //今天作弊添加的条数
        $today_start_time = strtotime(date('Y-m-d'));
        $today_end_time   = strtotime(date('Y-m-d').' 23:59:59');
        $today_cheat_total = M('vrnews1.vote_cheat')
            ->where("to_user_id={$vote_user_option_id} AND create_time>{$today_start_time} AND create_time<{$today_end_time}")
            ->count();

        if($today_cheat_total > $today_total) exit(1004);



        $redis = new Redis();
        $password = C("REDIS_PASSWORD");
        if($password){
            $redis->auth($password);
        }
        $next_exec_time = $redis->get("add_vote_cheat_time");
        if(!$next_exec_time){
            $next_exec_time = 5;
        }

        $last_add_time = M('vrnews1.vote_cheat')->where('1=1')->order('create_time DESC')->getField('create_time');

        if(time() < ($last_add_time + $next_exec_time * 60)) exit(1007);

        //vruser1找到很久不登陆的用户
        $exec_add_num  = mt_rand(2,3);
        $get_user_info = M('vruser1.user')->where("user_id NOT IN (SELECT user_id FROM vrnews1.vote_ballot WHERE option_id=619)")->order('login_time ASC')->limit($exec_add_num)->select();

        if(count($get_user_info) == 0) exit(1005);

        //插入投票表
        $vote_id = I('vote_id', 11);
        $option_id = I('option_id', 619);
        if(!$vote_id || !$option_id) exit(1003);

        $add_vote_data = array();
        foreach($get_user_info as $user){
            $add_vote_data = array('vote_id'=>$vote_id, 'option_id'=>$option_id, 'user_id'=>$user['user_id'], 'create_time'=>time());
            $add_vote_cheat_data = array('user_id'=>$user['user_id'], 'to_user_id'=>$option_id, 'create_time'=>time());

            $ret = M('vrnews1.vote_ballot')->add($add_vote_data);

            if($ret){
                M('vrnews1.vote_cheat')->add($add_vote_cheat_data);
            }
            sleep(mt_rand(3,6));
        }

        $redis->set("add_vote_cheat_time", mt_rand(1, 3));
    }

    public function VoteCheat(){
        $vote_total_sql = "
            SELECT
                c.class_name, o.option_id, o.`name`, COUNT(b.ballot_id) as vote_total
            FROM
                vrnews1.vote_ballot b LEFT JOIN
                vrnews1.vote_option o ON b.option_id=o.option_id LEFT JOIN
                vrnews1.vote_class  c ON o.group_id=c.class_id
            WHERE 
                b.vote_id={$this->vote_id}
            GROUP BY
                b.option_id
            ORDER BY
                vote_total DESC
            LIMIT 
                10
        ";

        $vote_total = M()->query($vote_total_sql);

        if(!$vote_total || empty($vote_total)) exit('2222');

        $option_total = 0;
        $total_temp_str = '';
        foreach ($vote_total as $key=>$option){
            $total_temp_str .= $option['vote_total'].',';
            if($option['option_id'] == $this->option_id){
                $option_total = $option['vote_total'];
            }
        }
        $total_temp_str = trim($total_temp_str, ',');
        $total_temp_arr = explode(',', $total_temp_str);
        $total_temp_arr = array_unique($total_temp_arr);

        $vote_ranking_arr = array();
        foreach($total_temp_arr as $item){
            $vote_ranking_arr[] = $item;
        }

        //如果已经是这个名次了 退出
        if($option_total['vote_total'] == $vote_ranking_arr[$this->ranking]) exit('2000');

        //取得想要名次的票数
        $ranking_total = $vote_ranking_arr[$this->ranking];

        //如果选手的票数大于想要名次的票数
        if($option_total > $ranking_total){
            exit('2001');
        }else if($option_total == $ranking_total){
            $exec_add_num = mt_rand(2, 3);
        }else{
            $exec_add_num  = $ranking_total - $option_total + mt_rand(2, 5);
        }

        //如果设置的不是第一名 不能比上个名次的票数多
        if($this->ranking > 0){
            //取出上一名和想要取得的名次 中间随机后 减去现有的票数
            $last_option_total = $vote_ranking_arr[$this->ranking - 1] - 1;
            $next_option_total = $vote_ranking_arr[$this->ranking] + 1;
            $need_vote_total   = mt_rand($next_option_total, $last_option_total);    // mt_rand(28, 29) 返回false
            if(!$need_vote_total){
                $exec_add_num = 2;
            }else{
                $exec_add_num = $need_vote_total - $option_total;
            }
            if($exec_add_num <= 0) exit('2224');
        }

        //vruser1找到很久不登陆的用户
        $user_info_arr = M('vruser1.user')->where("user_id NOT IN (SELECT user_id FROM vrnews1.vote_ballot WHERE option_id={$this->option_id})")->order('login_time ASC')->limit($exec_add_num)->select();

        if(!$user_info_arr || count($user_info_arr) == 0) exit('2224');

        $add_vote_data = array();
        foreach($user_info_arr as $user){
            $add_vote_data = array('vote_id'=>$this->vote_id, 'option_id'=>$this->option_id, 'user_id'=>$user['user_id'], 'create_time'=>time());
            $add_vote_cheat_data = array('user_id'=>$user['user_id'], 'to_user_id'=>$this->option_id, 'create_time'=>time());

            $ret = M('vrnews1.vote_ballot')->add($add_vote_data);

            if($ret){
                M('vrnews1.vote_cheat')->add($add_vote_cheat_data);
            }
            sleep(mt_rand(3,7));
        }
    }
}